package com.javaear.zblog4java.system.component;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.javaear.generalmapper.GeneralMapper;
import com.javaear.zblog4java.system.entity.dto.SettingDTO;
import com.javaear.zblog4java.system.entity.enums.ArticleStatusEnum;
import com.javaear.zblog4java.system.entity.model.CategoryModel;
import com.javaear.zblog4java.system.entity.model.ConfigModel;
import com.javaear.zblog4java.system.entity.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author aooer
 */
@Component
public class AppConfig {

    private static GeneralMapper generalMapper;

    @Autowired
    public void setGeneralMapper(GeneralMapper generalMapper) {
        AppConfig.generalMapper = generalMapper;
    }

    public static Map<String, Object> settingMap = new ConcurrentHashMap<>();

    public static void loadSetting() {
        SettingDTO settingDTO = JSON.parseObject(generalMapper.selectOne(new ConfigModel("settingDTO")).getContent(), SettingDTO.class);
        settingMap.put("App-deskPageSize", settingDTO.getDeskPageSize());
        settingMap.put("App-systemPageSize", settingDTO.getSystemPageSize());
        settingMap.put("App-blogTitle", settingDTO.getBlogTitle());
        settingMap.put("App-blogSubTitle", settingDTO.getBlogSubTitle());
        settingMap.put("App-categorys", generalMapper.selectList(new EntityWrapper<>(new CategoryModel())));
        settingMap.put("App-users", generalMapper.selectList(new EntityWrapper<>(new UserModel())));
        settingMap.put("App-article-status", ArticleStatusEnum.values());
    }
}
